package com.juli.school.service.impl;

import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.druid.util.StringUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.juli.school.dao.entity.TestSubject;
import com.juli.school.dao.mapper.TestSubjectMapper;
import com.juli.school.service.bo.TestSubjectBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;

/**
 * @author: hantao
 * @since: 2017年11月9日 
 */
@Service
public class TestSubjectService {
	
	private static final Logger logger = LoggerFactory.getLogger(TestSubjectService.class);
	
	@Resource
	TestSubjectMapper testSubjectMapper;
	
	public CommonResp<Boolean> add(TestSubjectBo testSubjectBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (testSubjectBo.getSnStamp() == null) {
				commonResp.fail("心理测试编号不可为空");
				return commonResp;
			}
			// 设置 起止日期
			testSubjectBo.setSubjectStamp(UUID.randomUUID().toString());
			testSubjectMapper.insert(testSubjectBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增题目信息异常", e);
			commonResp.exception("新增题目信息异常");
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> update(TestSubjectBo testSubjectBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (Strings.isNullOrEmpty(testSubjectBo.getSnStamp())) {
				commonResp.fail("心理测试编号不可为空");
				return commonResp;
			}
			testSubjectMapper.update(testSubjectBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("更新心理测试信息异常", e);
			commonResp.exception("更新心理测试信息异常");
		}
		return commonResp;
	}
	
	public CommonResp<Boolean> delete(String snStamp) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		try {
			if (StringUtils.isEmpty(snStamp)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			testSubjectMapper.deleteBySnStamp(snStamp);
			commonResp.success();
		} catch (Exception e) {
			logger.error("删除心理测试信息异常", e);
			commonResp.exception("删除心理测试信息异常");
		}
		return commonResp;
	}
	
	public CommonResp<TestSubject> detail(String subjectStamp) {
		CommonResp<TestSubject> commonResp = new CommonResp<TestSubject>();
		try {
			if (Strings.isNullOrEmpty(subjectStamp)) {
				commonResp.fail("入参不可为空");
				return commonResp;
			}
			TestSubject testSubjectBo = new TestSubject();
			testSubjectBo.setSubjectStamp(subjectStamp);
			TestSubject testSubject = testSubjectMapper.get(testSubjectBo);
			if (testSubject == null) {
				commonResp.exception("没有获取到信息");
				return commonResp;
			}
			BeanUtils.copyProperties(testSubjectBo, testSubject);
			commonResp.setData(testSubject);
			commonResp.success();
		} catch (Exception e) {
			logger.error("获取发布信息详情发生异常", e);
			commonResp.exception("获取发布信息详情发生异常");
		}
		return commonResp;
	}
	
	public CommonResp<List<TestSubjectBo>> listPage(String snStamp) {
		CommonResp<List<TestSubjectBo>> commonResp = new CommonResp<List<TestSubjectBo>>();
		try {
			if (Strings.isNullOrEmpty(snStamp)) {
				commonResp.fail("入参不能为空");
				return commonResp;
			}
			TestSubjectBo req = new TestSubjectBo();
			req.setSnStamp(snStamp);
			List<TestSubject> list = testSubjectMapper.list(req);
			List<TestSubjectBo> data = Lists.newArrayList();
			for (TestSubject tes : list) {
				TestSubjectBo testSubjectBo = new TestSubjectBo();
				BeanUtils.copyProperties(testSubjectBo, tes);
				data.add(testSubjectBo);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (Exception e) {
			logger.error("获取心理测试题目异常", e);
			commonResp.exception("获取心理测试题目异常");
		}
		return commonResp;
	}
}
